package com.lvandroid.app.transport;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.lvandroid.app.R;
import com.lvandroid.transport.DownloadManager;
import com.lvandroid.transport.download.IDownLoad;
import com.lvandroid.transport.task.IOnDownloadTaskUpdateListener;
import com.lvandroid.transport.task.TaskInfo;
import com.lvandroid.transport.task.TaskManager;

import java.util.List;

public class TransportTestActivity extends AppCompatActivity {

    private static final String TEST_URL = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201311%2F17%2F174124tp3sa6vvckc25oc8.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625887743&t=62f351e7af4d76fef9c2750269a78b92";
    private static final String TEST_URL_2 = "http://p.gdown.baidu.com/b960ff9b2b7ed1ba5bfa7a9cae06d12801c69645dc19e417dd2726c396d500809282dfdc15a5d7bbe35193d135f9205ff0fb4fc651024601cdb8c726abf16bce6db2b91a81a1fbc64dd4e14dc8d7931007f6ead877fffcec7f6df67905b4f9f2bd8ff049c2d5487c257875796bd6f3670cf0f11714877e9e6adae996321b320141c044a29c47391784d5ccfbf3f2936d929205a4c3729625c0f9e9aba05f188b67e205b65a9bf313";
    private TextView mTvProgress;
    private String mSavePath;
    private IDownLoad mDownLoader;
    private long mCurrentTaskId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transport);
        mTvProgress = findViewById(R.id.progress);
        mSavePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath();
        mSavePath += "/test.apk";
        mDownLoader = DownloadManager.getInstance().getDownLoader(IDownLoad.DOWNLOADER_HTTPS);
        Log.i("MainActivity", "save path : " + mSavePath + ", Donwloader : " + mDownLoader);
        mDownLoader.addDownLoadListener(new IOnDownloadTaskUpdateListener() {
            @Override
            public void onUpdate(TaskInfo task) {

            }

            @Override
            public void onComplete(TaskInfo task) {

            }
        });
    }

    public void doStart(View v) {
        mCurrentTaskId = mDownLoader.start(TEST_URL_2, mSavePath);
        Log.i("MainActivity", "doStart : " + mCurrentTaskId);
    }

    public void doPause(View v) {
        Log.i("MainActivity", "doPause : " + mCurrentTaskId);
        mDownLoader.stop(mCurrentTaskId);
    }

    public void doResume(View v) {
        Log.i("MainActivity", "doResume : " + mCurrentTaskId);
        mDownLoader.resume(mCurrentTaskId, false);
    }

    public void doQuery(View v) {
        List<TaskInfo> complete = TaskManager.getInstance().getAllCompleteDownloadTask();
        List<TaskInfo> notComplete = TaskManager.getInstance().getAllNotCompleteDownloadTask();
        if(null != complete && complete.size() > 0) {
            for (TaskInfo e : complete
            ) {
                Log.i("MainActivity", "doQuery : " + e.mTaskId + "  " + e.mTaskUrl + "   " + e.getFilePath());
            }
        }

        if(null != notComplete && notComplete.size() > 0) {
            for (TaskInfo e : notComplete
            ) {
                Log.i("MainActivity", "doQuery : " + e.mTaskId + "  " + e.mTaskUrl + "   " + e.getFilePath());
            }
        }
    }

    public void doRestart(View view) {
        List<TaskInfo> notComplete = TaskManager.getInstance().getAllNotCompleteDownloadTask();
        Log.i("MainActivity", "doRestart : " + notComplete);

        if(null != notComplete && notComplete.size() > 0) {
            mCurrentTaskId = notComplete.get(0).getTaskId();
            mDownLoader.resume(mCurrentTaskId, true);
        }
    }
}